from flask import Flask,redirect,url_for,render_template,request
import multiprocessing as mp
from time import sleep
from PIL import Image
from io import BytesIO
from sys import version_info
import courseAction,base64

app = Flask(__name__)

def run():
    global username
    username='20199463'
    password='ysf20010808..'
    course='1'
    admin='1'
    s = mp.Process(target=courseAction.run,args=(username,password,course,admin))
    s.start()
    with open("./log/"+username+'.log', 'a') as f:
        f.write("学号："+username+"\n成功运行，"+"进程号为: " + str(s.pid) + "\n")

@app.route('/')
def index():
    return '<iframe src="/code"" width="500" height="500" scrolling="no"></iframe>'

@app.route('/code')
def sendCode():
    username='20199463'
    img=Image.open('./admin/'+username+'.png')
    img = img.convert('RGB')
    buffered = BytesIO()
    img.save(buffered, format="JPEG")
    if version_info.major >= 3:
        b64 = str(base64.b64encode(buffered.getvalue()), encoding='utf-8')
    else:
        b64 = str(base64.b64encode(buffered.getvalue()))

    return render_template('code.html', plot_url=b64)

if __name__ == '__main__':
  app.run(host='127.0.0.1',port='888')